from PIL import Image

from thread_project.demo02.modules.base import BaseModule
from thread_project.demo02.modules.excutors import thread_pool_executor as tp
from thread_project.demo02.modules.excutors import process_pool_executor as pp


class Storager(BaseModule):
    def _process(self, item):
        content, path = item
        content = Image.fromarray(content.astype("uint8")).convert("RGB")
        content.save(path)

    def _process_single_thread(self, list_):
        for item in list_:
            self._process(item)

    def _process_multi_thread(self, list_):
        task_list = []
        for item in list_:
            task = tp.submit(self._process, (item,))
            task_list.append(task)
        for task in task_list:
            task.result()

    def _process_multi_process(self, list_):
        task_list = []
        for item in list_:
            task = pp.submit(self._process, (item))
            task_list.append(task)

        for task in task_list:
            task.result()
